package com.main;

import com.source.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Samuel on 2016/11/8.
 */
@Service
public class MainImpl implements MainFace {



    public Object testLink(String count) {
        Object object = new Object();
        switch (Integer.valueOf(count)){
            case 0:{
                object = addDocument();
            }
            case 1:{
                object = addDocuments();
            }
            case 2:{
                object = getDocument();
            }
            case 3:{
                object = delDocument();
            }
            case 4:{
                object = updateDocument();
            }
            case 5:{
                object = getDocuments_ik();
            }
        }
        return  object;
    }

    public String addDocument(){
        SElasticBase elasticBase = new SElasticBase();
        elasticBase.index = "sindex";
        elasticBase.type = "stype";
        elasticBase.id = "0";
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name","addDocument");
        elasticBase.setJsonMap(map);
        SElastic.addDocument(elasticBase);
        return "addDocument";
    }
    public String addDocuments(){
        List<SElasticBase> list = new ArrayList<SElasticBase>();
        for (int i=0;i<20;i++){
            SElasticBase elasticBean = new SElasticBase();
            elasticBean.index = "sindex";
            elasticBean.type = "stype";
            elasticBean.id = String.valueOf(i);
            Map<String, Object> map = new HashMap<String, Object>();
            if (i<4){
                map.put("name","中文测试"+String.valueOf(i)+"补充的文字");
            }
            else if (i<8){
                map.put("name","englishTest"+String.valueOf(i)+"test_confirm");
            }
            else if (i<12){
                map.put("name","中文，逗号测试"+String.valueOf(i)+"测策试文档，错别字");
            }
            else if (i<16){
                map.put("name","词汇测试"+String.valueOf(i)+"我看见啊！顶风冒雪看得见繁华过去");
            }
            else {
                map.put("name","中英混合englishMix"+String.valueOf(i)+"及and附近is的内心Open矛盾Come on");
            }
            map.put("number",Integer.valueOf(i));//需要被搜索引擎使用范围检索的字段必须是数据类型
            map.put("count",Integer.valueOf(21+i));//需要被搜索引擎使用范围检索的字段必须是数据类型
            elasticBean.setJsonMap(map);
            list.add(elasticBean);
        }
        SElastic.addDocuments(list);
        return "addDocuments";
    }
    public Object getDocument(){
        SElasticBase base = new SElasticBase();
        base.index = "sindex";
        base.type = "stype";
        base.id = "10";
        Map<String,Object> map = SElastic.getDocument(base);
        return map;
    }
    public String delDocument(){
        SElasticBase base = new SElasticBase();
        base.index = "sindex";
        base.type = "stype";
        base.id = "0";
        SElastic.delDocument(base);
        return "delDocument";
    }
    public String updateDocument(){
        SElasticBase base = new SElasticBase();
        base.index = "sindex";
        base.type = "stype";
        base.id = "0";
        SElasticTerm term = new SElasticTerm();
        term.key = "name";
        term.value = "数据被修改为：啦啦啦";
        List<SElasticTerm> list = new ArrayList<SElasticTerm>();
        list.add(term);
        base.terms = list;
        SElastic.updateDocument(base);
        return "updateDocument";
    }
    public Object getDocuments_ik(){
        SElasticBase base = new SElasticBase();
        /******** base ********/
        base.index = "sindex";
        base.type = "stype";
        base.pageIndex = 0;
        base.pageSize = 10;
        /******** query ********/
        List<SElasticSingle> singles = new ArrayList<SElasticSingle>();
        SElasticSingle single1 = new SElasticSingle();
        single1.value = "中文";
        single1.type = SESEnum.must;
        singles.add(single1);
        SElasticSingle single2 = new SElasticSingle();
        single2.value = "文字";
        single2.type = SESEnum.should;
        singles.add(single2);
//        base.singleList = singles;
        /******** term ********/
        List<SElasticTerm> termList = new ArrayList<SElasticTerm>();
        for (int i = 0;i<1;i++){
            SElasticTerm term = new SElasticTerm();
            term.key = "name";
            term.value = "逗号测试10";
            term.type = SESEnum.must;
            term.isPhrase = true;
            termList.add(term);
        }
        base.terms = termList;
        /******** range ********/
        List<SElasticRange> rangeList = new ArrayList<SElasticRange>();
        for (int k = 0;k<1;k++){
            SElasticRange range1 = new SElasticRange();
            range1.key = "number";
            range1.from = String.valueOf(8);
            range1.to = String.valueOf(9);
            range1.type = SESEnum.must;
            rangeList.add(range1);
            SElasticRange range2 = new SElasticRange();
            range2.key = "count";
            range2.from = String.valueOf(30);
            range2.to = String.valueOf(31);
            range2.type = SESEnum.must;
            rangeList.add(range2);
        }
//        base.ranges = rangeList;
        /******** field ********/
        List<String> fieldList = new ArrayList<String>();
        fieldList.add("count");
        base.fields = fieldList;
        /******** sort ********/
        List<SElasticSort> sortList = new ArrayList<SElasticSort>();
        SElasticSort sort = new SElasticSort();
        sort.isASC_DESC =false;
        sort.key = "count";
        sortList.add(sort);
        base.sorts = sortList;

        List<Map<String, Object>> list = SElastic.getDocuments_ik(base);
        return list;
    }

}
